	#include <p10f200.inc>        ; processor specific variable definitions

	#define	NO_PARAMS	H'03'

	__CONFIG   _MCLRE_OFF & _CP_OFF & _WDT_OFF

;----------------------------------
;cblock
;	var1
;	var2
;	var3
;endc

;counter1    equ     H'08'
;cnt10us	    equ     H'09'
;cnt100us	equ     H'0A'
;cnt10ms		equ     H'0B'
;counter2    equ     H'0C'
;waitcnt1    equ     H'0D'
;waitcnt2	equ     H'0E'
cnt1		equ     H'10'
cnt2		equ     H'11'
cnt3		equ     H'12'	;power-on power-off load-on load-off loading
state	    equ     H'13'	;universal counter
;frequency
timp	    equ     H'14'	;time of pulse high or low
ts		    equ     H'15'	;time of signal
tp		    equ     H'16'	;time of pause
;loaded sample from flash
;tshl        equ     H'18'   ;<0..7>time of H level of rectangle signal
;tshh        equ     H'19'   ;<8..16>time of H level of rectangle signal
;tsll        equ     H'1A'   ;<0..7>time of L level of rectangle signal
;tslh        equ     H'1B'   ;<8..16>time of L level of rectangle signal
;tsl         equ     H'1C'   ;time of signal
;tsh         equ     H'1D'   ;time of signal
;tpl         equ     H'1E'   ;time of pause
;tph         equ     H'1F'   ;
	
;----------------------------------
	RES_VECT	CODE    0x0000

    ;org   	0x1FF      ; processor reset vector
	;org   	0x000
	goto		init
	
;----------------------------------

;===============================================================================
MAIN_PROG CODE                      ; let linker place main program
;--------------------------------------------------------------------
;SOUND BANK AND ROUNTINES
;--------------------------------------------------------------------
event_map:

sample_bank:
	addwf	PCL
	;sample01
	retlw	D'44'
	retlw	D'10'
	retlw	D'3'
	;sample01
	retlw	D'42'
	retlw	D'10'
	retlw	D'3'
	;sample01
	retlw	D'40'
	retlw	D'10'
	retlw	D'3'
	;sample01
	retlw	D'37'
	retlw	D'20'
	retlw	D'3'
	;sample01
	retlw	D'44'
	retlw	D'30'
	retlw	D'3'
	;sample01
	retlw	D'44'
	retlw	D'10'
	retlw	D'3'
;----------------------------------
;LOADING SAMPLE
;----------------------------------
load_sample:
	movwf	cnt1
	movlw	timp					;set pointer to first register address
	movwf	FSR						;and set FSR register
	movlw	NO_PARAMS				;number of parameters
	movwf	cnt2					;load variable as counter
	movf	cnt1, W				;set to begining of given sample
load_next_parameter:
	call	sample_bank				;loading a piece of sample from bank
	movwf	INDF					;copying value of from bank to register
	incf	cnt1, F					;increment pointer to the element of given sample...
	movf	cnt1, W					;and copy given value to W (W is as function argument)
	incf	FSR,F					;increment the pointer of reg file
	decfsz	cnt2
	goto	load_next_parameter		;if not zero get next parameter
	retlw	0						;return
;----------------------------------
;GENERATING SOUND
;Sound output:
;GP0
;----------------------------------
generate_sound:
	movlw	b'10000001'				;setting of timer0 prescaler
	option
	movf	ts,W
	movwf	cnt2
	movlw	D'10'
	movwf	cnt3
signal_loop:
	bsf		GPIO,0
	movf	timp,W
	movwf	cnt1
high_impulse_delay:					;delay=4us+(timp*14us)
	call	wait10ust
	decfsz	cnt1
	goto	high_impulse_delay
	nop
	bcf		GPIO,0
	movf	timp,W
	movwf	cnt1
low_impulse_delay:
	call	wait10ust
	decfsz	cnt1
	goto	low_impulse_delay
	decfsz	cnt3
	goto	signal_loop
	movlw	D'10'					;hard coded delay parameter
	movwf	cnt3					;set low counter
	decfsz	cnt2					;decrement high counter, jump if zero
	goto	signal_loop				;if not zero
pause_loop_init:
	movf	tp,W					;copy tp parameter
	movwf	cnt1					;to cnt1 register
pause_loop:
	call	wait10mst				;call wait routine
	decfsz	cnt1
	goto	pause_loop

	retlw	0
;--------------------------------------------------------------------
;Universal delay procedures using timer0
;--------------------------------------------------------------------
wait10ust:
	movlw	b'10000001'				;setting of timer0 prescaler
	option
	movlw	d'0'
	movwf	TMR0
wait10ust_l1:
	movf	TMR0,W
	btfss	STATUS,Z
	goto	wait10ust_l1
	retlw	0
;----------------------------------
wait100ust:
	movlw	b'10000001'				;setting of timer0 prescaler
	option
	movlw	d'235'
	movwf	TMR0
wait100ust_l1:
	movf	TMR0,W
	btfss	STATUS,Z
	goto	wait100ust_l1
	nop
	nop
	retlw	0
;----------------------------------
wait10mst:
	movlw	b'10000101'				;setting of timer0 prescaler
	option
	movlw	d'102'
	movwf	TMR0
wait10mst_l1:
	movf	TMR0,W
	btfss	STATUS,Z
	goto	wait10mst_l1
	movlw	b'10000001'				;setting of timer0 prescaler
	option
	movlw	d'227'
	movwf	TMR0
wait10mst_l2:
	movf	TMR0,W
	btfss	STATUS,Z
	goto	wait10mst_l2
	nop
	nop
	nop
	retlw	0
;--------------------------------------------------------------------
;END OF DELAY PROCEDURES
;--------------------------------------------------------------------

;--------------------------------------------------------------------
;RECOGNIZING OF POWER STATE
;Inputs:
;GP3 - power control 1 (pc1)
;GP2 - power control 2 (pc2)
;--------------------------------------------------------------------

;--------------------------------------------------------------------
;--------------------------------------------------------------------

;initialization
init:

	movlw	000h
	movwf   OSCCAL            ; update register with factory cal value

	clrwdt
	clrf	TMR0
	MOVLW	b'10000111'
	OPTION

	clrf 	GPIO
	movlw 	b'00000000'
	tris 	GPIO
;--------------------------------------------------------------------
;MAIN PROGRAM
;--------------------------------------------------------------------
main:

	;call	wait10ust
	;call	wait100ust
	;call	wait10mst

	;call	wait10us;
	;call	wait100us
;	call	wait10ms
	movlw	D'00'
	call	load_sample
	call	generate_sound

	movlw	D'03'
	call	load_sample
	call	generate_sound

	movlw	D'06'
	call	load_sample
	call	generate_sound

	movlw	D'09'
	call	load_sample
	call	generate_sound

	movlw	D'12'
	call	load_sample
	call	generate_sound

	goto	main
;--------------------------------------------------------------------
	
	END
